//
//  MeetingRoomViewController.m
//  Weiweiyunshi
//
//  Created by 王译 on 17/1/22.
//  Copyright © 2017年 王译. All rights reserved.
//

#import "MeetingRoomViewController.h"
#import "MeetingRoomView.h"
#import "TestViewController.h"
#import "LoginModel.h"
#import "MeetingRoomModel.h"
#define MeetingRoomURL @"http://210.72.239.121:8088/SSIL/IndividualConf.do?userId=%@"

@interface MeetingRoomViewController ()
{
    LoginModel *loginModel;
    MeetingRoomModel *meetRoomModel;
    MeetingRoomView *view;
}
@end

@implementation MeetingRoomViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的会议室";
    loginModel = [LoginModel sharedInstance].userArr[0];
    view = [[MeetingRoomView alloc]init];
    self.view = view;
    [self statusRequest];
    [view.enterBtn addTarget:self action:@selector(enterMyRoon:) forControlEvents:UIControlEventTouchUpInside];
    [view.entryBtn addTarget:self action:@selector(entrtyPwd:) forControlEvents:UIControlEventTouchUpInside];
    [view.lockBtn addTarget:self action:@selector(lock:) forControlEvents:UIControlEventTouchUpInside];
    // Do any additional setup after loading the view.
}

-(void)statusForRoom{
   
    view.nameLabel.text = meetRoomModel.name;
    if ([meetRoomModel.status isEqualToString:@"free"]) {
        
        view.statusLabel.text = @"状态:空闲中";
    }else{
        view.statusLabel.textColor = [UIColor colorWithRed:246 green:201 blue:162];
        view.statusLabel.text = @"状态:与会中";
    }
    if ([meetRoomModel.confNum isEqualToString:@""]) {
        view.joinerNumLabel.text = @"与会人数:0";
    }else{
        view.joinerNumLabel.text = [NSString stringWithFormat:@"与会人数:%@",meetRoomModel.confNum];
    }
    
    [view.entryBtn setTitle:@"加密" forState:UIControlStateNormal];
    [view.entryBtn setTitle:@"解除密码" forState:UIControlStateSelected];
    [view.lockBtn setTitle:@"锁定" forState:UIControlStateNormal];
    [view.lockBtn setTitle:@"解锁" forState:UIControlStateSelected];
    [view.imgView sd_setImageWithURL:[NSURL URLWithString:meetRoomModel.img]];
}

-(void)statusRequest{
    
    NSString *urlStr = [NSString stringWithFormat:MeetingRoomURL,loginModel.userId];
    [WYHttpRequest getWithURLString:urlStr parameters:nil success:^(id responseObject) {
        
        meetRoomModel = [MeetingRoomModel yy_modelWithJSON:responseObject];
        NSLog(@"%@",meetRoomModel.status);
        [self statusForRoom];
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}

-(void)entrtyPwd:(UIButton *)button{
    if (button.selected == NO) {
        UIAlertController * alertController = [UIAlertController alertControllerWithTitle: @"加密" message: @"输入会议室密码" preferredStyle:UIAlertControllerStyleAlert];
        [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
            textField.placeholder = @"password";
            textField.textColor = [UIColor blueColor];
            textField.clearButtonMode = UITextFieldViewModeWhileEditing;
            textField.borderStyle = UITextBorderStyleRoundedRect;
            textField.secureTextEntry = YES;
        }];
        [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            NSArray * textfields = alertController.textFields;
            UITextField * passwordfiled = textfields[0];
            NSLog(@"%@",passwordfiled.text);
            NSString *entryPwdUrl = [NSString stringWithFormat:@"http://210.72.239.121:8088/SSIL/ReviseConfPwd.do?userId=%@&groupId=%@&confPwd=%@",loginModel.userId,meetRoomModel.groupID,passwordfiled.text];
            [WYHttpRequest getWithURLString:entryPwdUrl parameters:nil success:^(id responseObject) {
                if ([responseObject[@"Code"]isEqualToString:@"1"]) {
                    [MBProgressHUD showSuccessMessage:@"加密成功"];
                    button.selected = YES;
                }else{
                    [MBProgressHUD showErrorMessage:@"加密失败"];
                }
            } failure:^(NSError *error) {
                NSLog(@"error");
            }];
        }]];
        [self presentViewController:alertController animated:YES completion:nil];
        
    }else{
        NSString *cancelStr = [NSString stringWithFormat:@"http://210.72.239.121:8088/SSIL/CancelConfPwd.do?userId=%@&groupId=%@",loginModel.userId,meetRoomModel.groupID];
        [WYHttpRequest getWithURLString:cancelStr parameters:nil success:^(id responseObject) {
            if ([responseObject[@"Code"]isEqualToString:@"1"]) {
                [MBProgressHUD showSuccessMessage:@"取消成功"];
                button.selected = NO;
            }else{
                [MBProgressHUD  showErrorMessage:@"取消失败"];
            }
        } failure:^(NSError *error) {
            NSLog(@"%@",error);
        }];
    }
}

-(void)lock:(UIButton *)button{
    NSString *lockstr;
    if (button.selected == NO) {
        lockstr = @"2";
        button.selected = YES;
    }else{
        lockstr = @"1";
        button.selected = NO;
    }
    NSString *lockUrl = [NSString stringWithFormat:@"http://210.72.239.121:8088/SSIL/LockConf.do?isLocked=%@&userId=%@&groupId=%@",lockstr,loginModel.userId,meetRoomModel.groupID];
    [WYHttpRequest getWithURLString:lockUrl parameters:nil success:^(id responseObject) {
        if ([responseObject[@"Code"]isEqualToString:@"1"]) {
            [MBProgressHUD showSuccessMessage:@"操作成功"];
        }else{
            [MBProgressHUD showSuccessMessage:@"操作失败"];
        }
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}

-(void)enterMyRoon:(id)sender{
    TestViewController *testVc = [[TestViewController alloc]init];
    testVc.guestVRoomKey = loginModel.vidyoKey;
    [testVc showVidyo];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
